﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MetadataBasedConfigArch.Framework.Model;
using MetadataBasedConfigArch.Framework;

namespace MetadataBasedConfigArch.Model
{

    public partial class Tenant : ITenant
    {
    }

    public partial class ConfigType : IConfigType
    {
    }

    public partial class ConfigMeta : IConfigMeta
    {
        public IConfigType ConfigTypeInst
        {
            get
            {
                if (!ConfigType1Reference.IsLoaded)
                    ConfigType1Reference.Load();
                return ConfigType1;
            } 
        }

        protected List<IConfigMetaRelation> _RelationsInst;
        public IEnumerable<IConfigMetaRelation> RelationsInst
        {
            get
            {
                if (_RelationsInst == null)
                {
                    _RelationsInst = new List<IConfigMetaRelation>();
                    _RelationsInst.AddRange(ConfigMetaRelations);
                    _RelationsInst.AddRange(ConfigMetaRelations1);
                }
                return _RelationsInst;
            }
        }

        protected List<IConfigMetaConfigInfo> _ConfigInfosInst;
        public IEnumerable<IConfigMetaConfigInfo> ConfigInfosInst
        {
            get
            {
                if (_ConfigInfosInst == null)
                { 
                    _ConfigInfosInst = new List<IConfigMetaConfigInfo>();
                    _ConfigInfosInst.AddRange(ConfigMetaConfigInfoes);
                }
                return _ConfigInfosInst;
            }
        }
    }

    public partial class ConfigMetaConfigInfo : IConfigMetaConfigInfo
    {

        public IConfigMeta ConfigMetaInst
        {
            get
            {
                if (!ConfigMeta1Reference.IsLoaded)
                    ConfigMeta1Reference.Load();
                return ConfigMeta1;
            } 
        }

        public ITenant TenantInst
        {
            get
            {
                if (!TenantReference.IsLoaded)
                    TenantReference.Load();
                return Tenant;
            } 
        }


        protected List<IConfigMetaConfigInfoProperty> _ConfigInfoPropertiesInst;
        public IEnumerable<IConfigMetaConfigInfoProperty> ConfigInfoPropertiesInst
        {
            get
            {
                if (_ConfigInfoPropertiesInst == null)
                {
                    _ConfigInfoPropertiesInst = new List<IConfigMetaConfigInfoProperty>();
                    _ConfigInfoPropertiesInst.AddRange(ConfigMetaConfigInfoProperties);
                }
                return _ConfigInfoPropertiesInst;
            }
        }
    }

    public partial class ConfigMetaConfigInfoProperty : IConfigMetaConfigInfoProperty
    {
        private Type _ConfigurableValidatorType = null;
        protected Type ConfigurableValidatorType
        {
            get
            {
                if (_ConfigurableValidatorType == null && !String.IsNullOrEmpty(ValidatorType))
                {
                    _ConfigurableValidatorType = Type.GetType(ValidatorType);
                }
                return _ConfigurableValidatorType;
            }
        }

        public IConfigMetaConfigInfo ConfigInfoInst
        {
            get
            {
                if (!ConfigMetaConfigInfoReference.IsLoaded)
                    ConfigMetaConfigInfoReference.Load();
                return ConfigMetaConfigInfo;
            } 
        }

        public ITenant TenantInst
        {
            get
            {
                if (!TenantReference.IsLoaded)
                    TenantReference.Load();
                return Tenant;
            }
        }

        public bool ValidateConfigData(IConfigMetaConfigData configData)
        {
            if (ConfigurableValidatorType == null)
                return true;

            if (ConfigurableValidatorType != null && typeof(IConfigInfoPropertyValidator).IsAssignableFrom(ConfigurableValidatorType))
            {
                IConfigInfoPropertyValidator validator = Activator.CreateInstance(ConfigurableValidatorType) as IConfigInfoPropertyValidator;
                if (validator != null)
                {
                    return validator.IsValid(this, configData);
                }
            }

            return false;
        }


        public bool ValidateCanBeAssignableFrom(object value)
        {
            if (ConfigurableValidatorType == null)
                return true;

            if (ConfigurableValidatorType != null && typeof(IConfigInfoPropertyValidator).IsAssignableFrom(ConfigurableValidatorType))
            {
                IConfigInfoPropertyValidator validator = Activator.CreateInstance(ConfigurableValidatorType) as IConfigInfoPropertyValidator;
                if (validator != null)
                {
                    return validator.CanBeAssignableFrom(this, value);
                }
            }
            return false;
        }
    }

    public partial class ConfigMetaConfigData : IConfigMetaConfigData
    {
        public IConfigMetaConfigInfo ConfigInfoInst
        {
            get
            {
                if (!ConfigMetaConfigInfoReference.IsLoaded)
                    ConfigMetaConfigInfoReference.Load();
                return ConfigMetaConfigInfo;
            } 
        }        
    }

    public partial class ConfigMetaRelationType : IConfigMetaRelationType
    {

    }

    public partial class ConfigMetaRelation : IConfigMetaRelation
    {
        public IConfigMeta MainConfigMetaInst
        {
            get
            {
                if (!ConfigMetaReference.IsLoaded)
                    ConfigMetaReference.Load();
                return ConfigMeta;
            } 
        }

        public IConfigMeta RelatedConfigMetaInst
        {
            get
            {
                if (!ConfigMeta1Reference.IsLoaded)
                    ConfigMeta1Reference.Load();
                return ConfigMeta1;
            } 
        }

        public IConfigMetaRelationType RelationTypeInst
        {
            get
            {
                if (!ConfigMetaRelationTypeReference.IsLoaded)
                    ConfigMetaRelationTypeReference.Load();
                return ConfigMetaRelationType;
            } 
        }
    }
}
